package com.gdk.ksoft.ui.reporters;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.gdk.ksoft.R;
import com.gdk.ksoft.assembly.dbhelper.FreporterDbHelper;
import com.gdk.ksoft.ui.reporters.adapters.OrderStatementDetailAdapter;
import com.gdk.ksoft.assembly.utils.extensions.FormatUtils;
import com.gdk.ksoft.assembly.utils.system.SQLiteUtils;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.HashMap;
import java.util.List;

/**
 * 历史订单详情页面
 */
public class HistoryOrderStatementDetailActivity extends Activity {
    @ViewInject(R.id.phistoryorderstatementDetail_llOrderInfo)private LinearLayout llOrderInfo;//订单详细信息视图框
    @ViewInject(R.id.phistoryorderstatementDetail_txtIcon)private TextView txtIcon;//客户地图图标
    @ViewInject(R.id.phistoryorderstatementDetail_txtCustomerName)private TextView txtCustomerName;//客户名称
    @ViewInject(R.id.phistoryorderstatementDetail_txtCustomerAddr)private TextView txtCustomerAddr;//客户地址
    @ViewInject(R.id.phistoryorderstatementDetail_txtOrderNo)private TextView txtOrderNo;//单据号
    @ViewInject(R.id.phistoryorderstatementDetail_txtCount)private TextView txtCount;//商品计量
    @ViewInject(R.id.phistoryorderstatementDetail_txtKinds)private TextView txtKinds;//商品种类计量
    @ViewInject(R.id.phistoryorderstatementDetail_txtMoney)private TextView txtMoney;//金额数
    @ViewInject(R.id.phistoryorderstatementDetail_lstOrder)private ListView lstOrder;//商品列表
    private DbUtils dbUtils;//数据库操作对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history_order_statement_detail);
        ViewUtils.inject(this);
        dbUtils = SQLiteUtils.getInstance(this).getDbUtils();
        Intent intent = getIntent();
        if(intent!=null){
            String orderNo = intent.getStringExtra("orderNo");
            txtOrderNo.setText("单号："+orderNo);
            if(!TextUtils.isEmpty(orderNo))//获取订单商品和订单信息
                requestOrderStatementDetail(HistoryOrderStatementDetailActivity.this,dbUtils,orderNo);
        }
    }


    /**
     * 获取订单数据
     * @param orderNo
     */
    public void requestOrderStatementDetail(final Context context,final DbUtils dbUtils,final String orderNo){
        new AsyncTask<Void,Void,Void>(){
            private ProgressDialog dialog = null;
            public HashMap<String,Object> orderInfo = null;
            public List<HashMap<String,Object>> goodslist = null;
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                dialog = ProgressDialog.show(HistoryOrderStatementDetailActivity.this,"","加载中...");
            }

            @Override
            protected Void doInBackground(Void... params) {
                try{
                    orderInfo = FreporterDbHelper.findOrderDetailByOrderNo(dbUtils, orderNo);
                    goodslist = FreporterDbHelper.findAllGoodsByOrderNo(dbUtils,orderNo);
                }catch (Exception e){
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                if(dialog!=null && dialog.isShowing()) dialog.dismiss();
                if(orderInfo!=null){
                    txtIcon.setText(orderInfo.get("kico").toString());
                    txtCustomerName.setText(orderInfo.get("kmc").toString());
                    txtCustomerAddr.setText("地址："+orderInfo.get("kaddr").toString());
                    txtCount.setText(orderInfo.get("xssl")+"件"+orderInfo.get("xsss")+"散");
                    txtKinds.setText(orderInfo.get("kinds")+"种");
                    txtMoney.setText(FormatUtils.exactNumber(orderInfo.get("xssy"), "0.00")+"元");
                }else{
                    llOrderInfo.setVisibility(View.GONE);
                }
                if(goodslist!=null){//获取商品列表
                    lstOrder.setAdapter(new OrderStatementDetailAdapter(HistoryOrderStatementDetailActivity.this,goodslist));
                }
            }
        }.execute();
    }

    @OnClick(value={R.id.phistoryorderstatementDetail_txtBack})
    public void onBackClick(View view){
        this.finish();
        overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {
            this.finish();
            overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
